import java.util.Random;

public class ThreadDemo12 {
    public static void main(String[] args) {
        ThreadGroup group=new ThreadGroup("thread-group");

        Runnable runTask=new Runnable() {
            @Override
            public void run() {//业务（任务）
                //生成一个1-3秒的随机数
                int num=(1+new Random().nextInt(3));
                try {
                    //跑了n秒之后到达终点
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Thread t=Thread.currentThread();
                System.out.println(t.getName()+"选手到达终点："+num+"s");
            }
        };
        //线程（运动员）
        Thread t1=new Thread(group,runTask);//运动员1
        Thread t2=new Thread(group,runTask);//运动员2
        Thread t3=new Thread(group,runTask);//远低于3
        //开跑
        t1.start();
        t2.start();
        t3.start();
        //所有人到达终点之后宣布成绩
        while(group.activeCount()>0){

        }
        System.out.println("宣布比赛成绩");
    }
}
